<?php

/**
 * 广告统计
 * File Name：GuestAdModel.php
 * File Encoding：UTF-8
 * File New Time：2014-9-2 15:48:52
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class GuestAdModel extends BaseModel {

    //广告标识
    public $adList = array();

    public function tableName() {
        return '{{guest_ad}}';
    }

    public function rules() {
        return array(
            array('adname,years,month,day,view,userfanw,youkefanw,kview,dip,uv,created_time', 'safe'),
        );
    }

    //数据保存前操作
    protected function beforeSave() {
        if ($this->isNewRecord) {
            $this->created_time = time();
        }
        return true;
    }

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 是否通过广告来的链接
     * @param type $url 当前访问地址
     * @return boolean
     */
    public function isAd($url) {
        if (empty($url)) {
            return '';
        }
        $parse = @parse_url($url);
        if (empty($parse) || empty($parse['query'])) {
            return '';
        }
        $this->adList = GuestAdlistModel::model()->getAdList();
        parse_str($parse['query'], $query);
        if (isset($query['tj']) && isset($this->adList[$query['tj']])) {
            return $query['tj'];
        }
        return '';
    }

    /**
     * 是否AD访问
     * @return type
     */
    public function isAdAccess() {
        $key = 'guest_' . GuestDataModel::model()->getMarkId();
        return Yii::app()->cache->get($key);
    }

    /**
     * 执行分析
     * @param type $start_time 开始时间
     * @param type $end_time 结束时间
     * @return boolean
     */
    public function sendFenxi($start_time = '', $end_time = '') {
        set_time_limit(400);
        if (empty($start_time)) {
            $start_time = strtotime(date('Y-m-d 00:00:00'));
        }
        if (empty($end_time)) {
            $end_time = strtotime(date('Y-m-d 23:59:59'));
        }
        $model = GuestDataModel::model();
        $where = array(
            'isad' => 1,
        );
        //获取需要查询广告ad列表 RedisCluster::getInstance()->push('snsNotice', $data);
        $key = 'GuestAdModel_sendFenxi_adList';
        $cacheAdList = RedisCluster::getInstance()->pop($key);
        $adList = array();
        if (empty($cacheAdList)) {
            foreach (GuestAdlistModel::model()->getAdList() as $admark => $adname) {
                $data = array($admark => $adname);
                RedisCluster::getInstance()->push($key, $data);
            }
            return true;
        } else {
            $adList = $cacheAdList;
        }
        foreach ($adList as $name => $value) {
            echo $name;
            echo "<br/>";
            unset($this->id);
            $where['adname'] = $name;
            $where['access_time'] = array(array('EGT', $start_time), array('ELT', $end_time), 'AND');
            //访问总数
            $view = $model->count($model->where($where));
            //会员访问数
            $userfanw = $model->count($model->where(array_merge($where, array('uid' => array('GT', 0)))));
            //游客访问数
            $youkefanw = $view - $userfanw;
            //K页面访问数
            unset($where['uid']);
            $kview = $model->count($model->where(array_merge($where, array('action' => 'skip'))));
            //独立ip
            $dipWhere = $model->where($where);
            $dipWhere->group = 'ip';
            $dip = (int) $model->count($dipWhere);
            //UV统计
            $uvWhere = $model->where($where);
            $uvWhere->group = 'session_id';
            $uv = (int) $model->count($uvWhere);
            //数据
            $data = array();
            $data['adname'] = $name;
            $data['years'] = (int) date('Y', $end_time); //年
            $data['month'] = (int) date('m', $end_time); //月
            $data['day'] = (int) date('d', $end_time); //日
            $data['view'] = $view;
            $data['userfanw'] = $userfanw;
            $data['youkefanw'] = $youkefanw;
            $data['kview'] = $kview;
            $data['dip'] = $dip;
            $data['uv'] = $uv;
            print_r($data);
            echo "<br/>";
            $info = $this->find($this->where(array('adname' => $name, 'years' => $data['years'], 'month' => $data['month'], 'day' => $data['day'])));
            if (!empty($info)) {
                $info->attributes = $data;
                $info->save();
            } else {
                $this->attributes = $data;
                $this->setIsNewRecord(true);
                $this->save();
            }
        }
        return true;
    }

}
